package com.personal.ifanya.presentation.model;

import com.alibaba.fastjson.JSON;

public class AsynResult<T> {

	private boolean success;
	
	private String errorMessage;
	
	private int errorCode;
	
	private T result;
	
	public static enum Error
	{
		NONE(0,"Success."),
		TIMEOUT(1,"Time out."),
		NORIGHT(2,"No right to do this operation."),
		FAILED(9,"Failed.");
		
		final private int code;
		final private String errorMessage;
		
		private Error(int code, String errorMessage)
		{
			this.code = code;
			this.errorMessage = errorMessage;
		} 
	}
	

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}
	
	public void setAll(boolean success, T result, Error error){
	
	 	this.setErrorCode(error.code);
		this.setErrorMessage(error.errorMessage);
		this.setResult(result);
		this.setSuccess(success);
    }
	
	public void setAll(boolean success, T result, Error error, String extraMessage){
		
	 	this.setErrorCode(error.code);
		this.setErrorMessage(error.errorMessage+"; More Details:"+extraMessage);
		this.setResult(result);
		this.setSuccess(success);
    }
	
	@Override
	public String toString()
	{
		return JSON.toJSONString(this);
	}
	
	public static void main(String[] args) {
		
		AsynResult<Integer> result = new AsynResult<Integer>();
		
		result.setErrorCode(0);
		result.setErrorMessage(null);
		result.setResult(null);
		result.setSuccess(true);
		
		System.out.println(JSON.toJSON(result));
		
	}
	
}
